package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.CourseOrder;
import com.biostime.qdingding.modules.qd.service.CourseOrderService;
import com.biostime.qdingding.common.persistence.Page;

/**
 * 课程交易Controller
 * @author chenweicheng
 * @version 2016-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/courseorder")
public class CourseOrderController extends BaseController {

	@Autowired
	private CourseOrderService courseorderService;
	
	@ModelAttribute
	public CourseOrder get(@RequestParam(required=false) String id) {
		CourseOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = courseorderService.get(id);
		}
		if (entity == null){
			entity = new CourseOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:courseorder:view")
	@RequestMapping(value = {"list", ""})
	public String list(CourseOrder courseorder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CourseOrder> page = courseorderService.findPage(new Page<CourseOrder>(request,response), courseorder);
		model.addAttribute("page", page);
		return "modules/qd/courseorderList";
	}

	@RequiresPermissions("qd:courseorder:view")
	@RequestMapping(value = "form")
	public String form(CourseOrder courseorder, Model model) {
		model.addAttribute("courseorder", courseorder);
		return "modules/qd/courseorderForm";
	}

	@RequiresPermissions("qd:courseorder:edit")
	@RequestMapping(value = "save")
	public String save(CourseOrder courseorder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, courseorder)){
			return form(courseorder, model);
		}
		courseorderService.save(courseorder);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/courseorder/?repage";
	}
	
	@RequiresPermissions("qd:courseorder:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, CourseOrder courseorder, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				courseorderService.delete(new CourseOrder(id));
			}
		}else{
			courseorderService.delete(courseorder);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/courseorder/?repage";
	}

}
